<?php

/*
 *
 *       _/_/_/                      _/        _/_/_/_/_/
 *    _/          _/_/      _/_/    _/  _/          _/      _/_/      _/_/
 *   _/  _/_/  _/_/_/_/  _/_/_/_/  _/_/          _/      _/    _/  _/    _/
 *  _/    _/  _/        _/        _/  _/      _/        _/    _/  _/    _/
 *   _/_/_/    _/_/_/    _/_/_/  _/    _/  _/_/_/_/_/    _/_/      _/_/
 *
 *
 *  Copyright 2013-2014, Geek Zoo Studio
 *  http://www.ecmobile.cn/license.html
 *
 *  HQ China:
 *    2319 Est.Tower Van Palace
 *    No.2 Guandongdian South Street
 *    Beijing , China
 *
 *  U.S. Office:
 *    One Park Place, Elmira College, NY, 14901, USA
 *
 *  QQ Group:   329673575
 *  BBS:        bbs.ecmobile.cn
 *  Fax:        +86-10-6561-5510
 *  Mail:       info@geek-zoo.com
 */

$_CFG['shop_reg_closed'] && abort(3);

($smscode = _POST('smscode')) || abort(4);
($mobile_phone = _POST('mobile_phone')) || abort(5);

$GLOBALS['db']->getOne('SELECT user_id FROM '.$GLOBALS['ecs']->table('users')." WHERE mobile_phone='{$mobile_phone}'") && abort(10);

($password = _POST('password')) || abort(15);
try {
    sms_verify($mobile_phone, $smscode) || abort(7);
} catch (Exception $e) {
    abort($e->getMessage());
}


check_mobile_phone($mobile_phone) || abort(18);
check_password($password) || abort(9);

$username = create_username();
$email = create_email($username);

function create_username()
{
    $seed = IP.ACCESS_START_TIME.$_SERVER['REMOTE_PORT'].rand();
    return substr(md5($seed), rand(0, 21), 10);
}

function create_email($username)
{
    return $username.'@aoyun.cv';
}

function check_smscode($smscode)
{
    return true;
    return $smscode === session('smscode');
}

function check_mobile_phone($mobile_phone)
{
    return preg_match('/^1[0-9]{10}$/', $mobile_phone);
}

function check_password($password)
{
    $password_length = strlen($password);
    return $password_length > 5 && $password_length < 11;
}

#include_once(EC_PATH . '/includes/lib_order.php');
include_once(EC_PATH . '/includes/lib_passport.php');

register($username, $password, $email, ['mobile_phone' => $mobile_phone]) === false && abort($GLOBALS['err']->last_message());

/*把新注册用户的扩展信息插入数据库*/
$sql = 'SELECT id FROM ' . $ecs->table('reg_fields') . ' WHERE type = 0 AND display = 1 ORDER BY dis_order, id';   //读出所有自定义扩展字段的id
$fields_arr = $db->getAll($sql);

$extend_field_str = '';    //生成扩展字段的内容字符串
foreach ($fields_arr AS $val)
{
    $extend_field_index = $val['id'];
    if(!empty($filelds[$extend_field_index]))
    {
        $temp_field_content = strlen($filelds[$extend_field_index]) > 100 ? mb_substr($filelds[$extend_field_index], 0, 99) : $filelds[$extend_field_index];
        $extend_field_str .= " ('" . $_SESSION['user_id'] . "', '" . $val['id'] . "', '" . $temp_field_content . "'),";
    }
}
$extend_field_str = substr($extend_field_str, 0, -1);

if ($extend_field_str)      //插入注册扩展数据
{
    $sql = 'INSERT INTO '. $ecs->table('reg_extend_info') . ' (`user_id`, `reg_field_id`, `content`) VALUES' . $extend_field_str;
    $db->query($sql);
}

return [];
